#include <string>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <errno.h>
#include <string.h>
#include <strings.h>  //for bzero
using namespace std;

#ifndef _SOCKSERVER_H_
#define _SOCKSERVER_H_

//the maximum length of the queue of pending connections
const int max_listen_backlog = 3;

class SockServer{
	public:
		SockServer();
		~SockServer();

		/* create socket descriptor */	
		bool Socket();

		bool SetAddr(short port);

		/* build connection */
		bool Bind();
		bool Connect();	
		bool Listen(int backlog=max_listen_backlog);	
		int Accept();

		/* message communication */
		bool Send();
		bool Receive();
		
		string GetErr(){
			return errMessage;	
		}
	public:
		bool StartServer(int port);
		
	private:
		void Err(){
			errMessage = strerror(errno);
		}
	private:
		int sockfd,clifd;
		struct sockaddr_in servaddr;
		struct sockaddr_in cliaddr;
		socklen_t servaddrlen;
		socklen_t cliaddrlen;
		char *inBuf,*outBuf;
		string errMessage;
};

#endif
